package com.tboss.template.sample.dwr.treeviewService;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.tboss.template.sample.dwr.treeviewDao.TreeviewDao;
import com.tboss.template.sample.dwr.treeviewDto.TreeviewDto;
import com.tboss.global.utils.useful.FileManager;

public class TreeviewJsonFileService implements TreeviewService {

	@Override
	public StringBuffer build(HashMap<String, String> opts){
		StringBuffer rtnValue = new StringBuffer();
		
		String rootKey = opts.get("jsonRootKey");
		String dataPath = opts.get("treeDataFile");
		List<TreeviewDto> list = new LinkedList<TreeviewDto>();
		InputStream is = null;
		InputStreamReader isr = null;
		BufferedReader br = null;

		try {
			if(dataPath != null && dataPath.length() > 0){
				URL url = this.getClass().getResource("/").toURI().toURL();
				String cp = new FileManager().malformedURL(url);
				dataPath = cp.substring(0, cp.indexOf("/WEB-INF")) + dataPath;
			}
			
			is = new FileInputStream(dataPath);
			isr = new InputStreamReader(is); 
			br = new BufferedReader(isr);
			
			String line = "";
			while((line = br.readLine()) != null){
				String tmp = line;
				tmp = tmp.replaceAll("\\s", "");
				tmp = tmp.replaceAll("\t", "");
				if(tmp.startsWith("#") || tmp.length() == 0) continue;
				
				//10 fields: index	belong	name	url	desc	 owner	group	use	collapse	order
				String[] fields = line.split("	");				
				TreeviewDto dto = new TreeviewDto();
				for(int i = 0; i < fields.length; i++){
					String field = ("null".equals(fields[i])) ? "" : fields[i];					
					switch(i){
						case 0 : dto.setFnId(field); break;
						case 1 : dto.setFnParentId(field); break;
						case 2 : dto.setFnAlias(field); break;
						case 3 : dto.setFnServletUrl(field); break;
						case 4 : dto.setFnDesc(field); break;
						case 5 : dto.setFnOwner(field); break;
						case 6 : dto.setFnGroup(field); break;
						case 7 : dto.setFnUseYN(field); break;
						case 8 : if(!"".equals(field)) dto.setFnCollapse(Boolean.parseBoolean(field)); break;
						case 9 : if(!"".equals(field)) dto.setFnOrder(Integer.parseInt(field)); break;
					}
				}
				
				//Numbering for name.
				//String name = (dto.getOrder()+1) + ". " + dto.getName();
				//dto.setName(name);
				
				list.add(dto);
			}

			Map<String, List<TreeviewDto>> menu = new HashMap<String, List<TreeviewDto>>();
			menu.put(rootKey, list);

			rtnValue.append(new ObjectMapper().writeValueAsString(menu));
			
		} catch (JsonGenerationException e) { e.printStackTrace();
		} catch (JsonMappingException e) { e.printStackTrace();
		} catch (IOException e) { e.printStackTrace();
		} catch (URISyntaxException e) { e.printStackTrace();
		} finally {
			try {
				if(br != null) br.close();
				if(isr != null) isr.close();
				if(is != null) is.close();
			} catch (IOException e) {e.printStackTrace();
			}
		}
		
		return rtnValue;
	}

	@Override
	public StringBuffer build(HashMap<String, String> opts, TreeviewDao treeviewDao) {
		return null;
	}
	
}
